package com.leaf.lowcode.controller;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.leaf.lowcode.entity.Xfcurrent;
import com.leaf.lowcode.mapper.XfFloorMapper;
import com.leaf.lowcode.mapper.XfcurrentMapper;
import com.leaf.lowcode.utils.RegularInstance;
import com.tencent.welink.iot.SubDeviceDTO;
import com.tencent.welink.iot.WelinkHttp;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 消防用电
 */

@Component
@RabbitListener(queues="treeMeter_queue")
public class SendDatatreeMeter {
    @Autowired
    private XfcurrentMapper xfcurrentMapper;
    @Autowired
    private XfFloorMapper xfFloorMapper;

    @RabbitHandler
    public void sendWDate(JSONObject treeMeter) {
        String deviceCode = (String) treeMeter.get("deviceCode");
        String floor = xfFloorMapper.selectfloor(deviceCode);
        //System.out.println("floor"+floor);
      //  String deviceCodeT = deviceCode + "T";
        String dataType = (String) treeMeter.get("dataType");
       // System.out.println("进入消息队列");
        //System.out.println("dataType" + dataType);
        List<SubDeviceDTO> subDeviceDTOList = RegularInstance.subDeviceDTOList;
        if (subDeviceDTOList != null) {
            for (SubDeviceDTO dto : subDeviceDTOList) {
                String pid = dto.getPid();
                String sn = dto.getSn();
                String din = dto.getDin();
                if (deviceCode.equals(sn)) {
                    JSONObject standardData = new JSONObject();
                    JSONObject profile = new JSONObject();
                    WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                    JSONObject properties = new JSONObject();
                    JSONObject propertie = new JSONObject();
                    if (dataType.equals("REAL_DATA_1")) {
                        System.out.println("传入数据1");
       //                 System.out.println("treeMeter"+treeMeter);
                        standardData.put("reportTs", System.currentTimeMillis());
                        propertie.put("deviceCode", deviceCode);
                        profile.put("modelId", 200637);
                        profile.put("poiCode", "w0105003");
                        standardData.put("profile", profile);
                        HashMap<String, Double> data = (HashMap<String, Double>) treeMeter.get("data");
                        Double mutuallyCurrentV = data.get("mutuallyCurrentV");
                        Double mutuallyVoltageV = data.get("mutuallyVoltageV");
                        Double electricityProduction=data.get("activePowerS")/1000;

                        System.out.println("=====实时能耗kw======"+electricityProduction);
                        propertie.put("mutuallyCurrentV", mutuallyCurrentV);
                        propertie.put("mutuallyVoltageV", mutuallyVoltageV);
                        propertie.put("electricityProduction",electricityProduction);

                        propertie.put("floor",floor);
                        Xfcurrent xfcurrent = new Xfcurrent();
                        xfcurrent.setDate(new Date());
                        xfcurrent.setDeviceCode(deviceCode);
                        xfcurrent.setSumElectricityProduction(electricityProduction);
                        xfcurrent.setMutuallyCurrentV(mutuallyCurrentV);
                        xfcurrentMapper.insert(xfcurrent);
                        List<String> sumelectr = xfcurrentMapper.sumElectricityProduction(deviceCode);
                        //TODO    i < sumcount.size()
                        for (int i = 0; i <1; i++) {
                            System.out.println("i==="+i);
                            double sumday = Double.parseDouble(sumelectr.get(i));
                            if(i==0)
                            {
                                System.out.println("第一天总数"+sumday);
                            }
                            System.out.println("所在楼层"+floor);
                            propertie.put("sumElectricityProductionday1", sumday);
                            System.out.println("今日能耗总能耗===="+sumday);
                        }
                        properties.put("smr1210v22info", propertie);
                        standardData.put("properties", properties);
                        standardData.put("events", new JSONObject());
                        try {
                            System.out.println("上传数据");
                            String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                            System.out.println("发送设备运行时事件数据==" + devRunEvent);
                            System.out.println("--------------------------------开始传输设备运行时事件数据");
                            wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                            System.out.println("数据1成功");
                        } catch (Exception e) {
                            System.out.println("设备运行时事件数据传输出现异常");
                        }
                    }
                } /*else if (deviceCodeT.equals(sn)) {
                   // System.out.println("传入数据2");
                    if (dataType.equals("REAL_DATA_2")) {
                        JSONObject standardData = new JSONObject();
                        standardData.put("reportTs", System.currentTimeMillis());
                        JSONObject profile = new JSONObject();
                        WelinkHttp wh = new WelinkHttp(RegularInstance.registerData1, RegularInstance.loginData);
                        JSONObject properties = new JSONObject();
                        JSONObject propertie = new JSONObject();
                        propertie.put("deviceCode", deviceCodeT);
                        profile.put("poiCode", "w0105003");
                        profile.put("modelId", 200638);
                        standardData.put("profile", profile);
                        propertie.put("floor",floor);
                        HashMap<String, Double> data = (HashMap<String, Double>) treeMeter.get("data");
                        System.out.println("data" + data);
                        Double temperatureN = data.get("temperatureN");
                        //湿度
                        propertie.put("ambientTemperature", temperatureN);
                        properties.put("smr1210v22temperature", propertie);
                        standardData.put("properties", properties);
                        standardData.put("events", new JSONObject());
                        try {
                           *//*          System.out.println("上传数据");
                            String devRunEvent = JSONObject.toJSONString(standardData, SerializerFeature.WriteMapNullValue);
                               System.out.println("发送设备运行时事件数据==" + devRunEvent);
                               System.out.println("--------------------------------开始传输设备运行时事件数据");
                            wh.sendMessage(RegularInstance.loginData.load(), 80000, Long.parseLong(din), devRunEvent, true);
                            System.out.println("数据2成功");*//*
                        } catch (Exception e) {
                                 System.out.println("设备运行时事件数据传输出现异常");
                        }
                    }
                }*/
            }
        }
    }
}